
package com.udc.medLine.factory;

import java.util.List;

import com.udc.medLine.entity.CondicaoVO;
import com.udc.medLine.entity.Status;
import com.udc.medLine.exception.BaseException;
import com.udc.medLine.exception.CampoDuplicadoException;
import com.udc.medLine.persistence.StatusDAO;

public class FactoryStatus {

	private StatusDAO statusDAO;

	/**
	 * 
	 */
	public FactoryStatus() {
		super();
		statusDAO = new StatusDAO();
		// TODO Stub de construtor gerado automaticamente
	}

	public Status getStatus() {
		return (statusDAO.getStatus());
	}

	public void setStatus(Status new_status) {
		this.statusDAO.setStatus(new_status);
	}

	public void insert() throws Exception {

		int size = statusDAO.search(
				"From status in class Status WHERE status.nome = '"
						+ statusDAO.getStatus().getNome()+"'").size();

		if (size > 0) {
			throw new CampoDuplicadoException("nome");
		}
		statusDAO.insert();
	}

	public void delete() throws Exception {
		int size = statusDAO.search(
				"From status in class Status ,Modelo as m WHERE m.status.id = "
						+ statusDAO.getStatus().getId()).size();

		if (size > 0) {
			throw new BaseException("referencia");
		}
		statusDAO.delete();
	}

	public void update() throws Exception {
		int size = statusDAO.search(
				"From status in class Status WHERE status.nome = '"
						+ statusDAO.getStatus().getNome() + "' and status.id != "
						+ statusDAO.getStatus().getId()).size();
		if (size > 0){
			throw new CampoDuplicadoException("nome");
		}
		statusDAO.update();
	}

	public List getStatus(String campo, String valor){
		List lista;
		try {
			lista = statusDAO.search("From status in class Status WHERE "+campo+" ='"+ valor + "'");
		}
		catch (Exception e){
			return null;
		}
		return lista;
	}
	
	public List getListaStatus(){		
		List lista;	
		try{		
			lista = statusDAO.search("From status in class Status");
		}
		catch (Exception e){
		 	return null;
		}
		return lista;		
	}
	
	public Status obterStatus() throws Exception {
		return statusDAO.retrive();
	}
	
	public List search(CondicaoVO condicao) throws Exception {
		String hql = "From status in class Status WHERE ";
		String campoSQL = null;
		String operadorSQL = null;
		String valorSQL = "'" + condicao.getValor() + "'";
		if (condicao.getCampo().equals("id")) {
			valorSQL = condicao.getValor();
			campoSQL = " id ";
			if (condicao.getOperador().equals("contem")) {
				condicao.setOperador("igual");				
			}
		} else if (condicao.getCampo().equals("nome")) {
			campoSQL = " nome ";
		}
		if (condicao.getOperador().equals("igual")) {
			operadorSQL = "=";
		} else if (condicao.getOperador().equals("diferente")) {
			operadorSQL = "!=";
		} else if (condicao.getOperador().equals("maior")) {
			operadorSQL = ">";
		} else if (condicao.getOperador().equals("menor")) {
			operadorSQL = "<";
		} else if (condicao.getOperador().equals("contem")) {
			operadorSQL = "like";
			valorSQL = "('%" + condicao.getValor() + "%')";
		}
		hql += campoSQL + " " + operadorSQL + valorSQL;
		return  statusDAO.search(hql);	
	}

}
